<template>
  <div class="page">

    <!-- 搜索区域 开始 -->
    <div class="el_card_box">
      <el-form :inline="true" size="small" :model="formInline" class="search_form">
        <el-form-item label="来源：">
          <user-source v-model="formInline.channelSources"></user-source>
        </el-form-item>
        <el-form-item label="广告代号：">
          <adcode-select v-model="formInline.adSources"></adcode-select>
        </el-form-item>
        <el-form-item label="创建人：">
          <user-select v-model="formInline.principals"></user-select>
        </el-form-item>
        <el-form-item label="创建时间：">
          <el-date-picker v-model="formInline.orderTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="关键词：">
          <el-input v-model="formInline.term" placeholder="请输入活动名称"></el-input>
        </el-form-item>
      </el-form>
      <div class="search_btn_box">
        <el-button type="primary" size="small" @click="getList(1)">筛选</el-button>
        <el-button type="default" size="small" @click="rest">清空筛选</el-button>
      </div>
    </div>
    <!-- 搜索区域 end -->

    <!-- 表格 开始 -->
    <div class="table_box">
      <!-- 操作区域 开始 -->
      <div class="action_box">
        <el-button type="primary" size="small" @click="add('add')">新增活动</el-button>
        <el-button type="default" size="small" :loading="eloading" @click="downloadExcel()">筛选批量导出</el-button>
      </div>
      <!-- 操作区域 end -->

      <el-table ref="elTable" class="el_table" size="small" :data="tableData">
        <el-table-column prop="name" label="活动名称" show-overflow-tooltip />
        <el-table-column prop="link" label="链接" width="240px" show-overflow-tooltip>
          <template slot-scope="{row}">
            <el-link type="primary" @click="copyLink(row)">{{row.link}}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="principal" label="二维码" show-overflow-tooltip>
          <template slot-scope="{row}">
            <el-link type="primary" @click="downloadQrcode(row)">下载</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="visitorCnt" label="访问次数" show-overflow-tooltip />
        <el-table-column prop="uniqueVisitorCnt" label="访问人数" show-overflow-tooltip />
        <el-table-column prop="enrollCnt" label="报名人数" show-overflow-tooltip />
        <el-table-column prop="channelSource" label="来源" show-overflow-tooltip />
        <el-table-column prop="adSource" label="广告代号" show-overflow-tooltip />
        <el-table-column prop="createdDate" label="创建时间" show-overflow-tooltip />
        <el-table-column prop="creator" label="创建人" show-overflow-tooltip />
        <el-table-column prop="address" label="操作" width="160" align="center" fixed="right">
          <template slot-scope="{row}">
            <el-link type="primary" class="el_link" @click="add('edit',row.id)">编辑</el-link>
            <el-link type="primary" class="el_link" @click="add('add',row.id)">复制</el-link>
            <el-link type="danger" class="el_link" @click="remove(row)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination_list">
        <el-pagination background layout="sizes, total,prev, pager, next, jumper" :total="pageTotal" :page-size="pageSize" :page-sizes="pageSizes" :current-page.sync="pageNumber" @current-change="getList" @size-change="changePageSize" />
      </div>
    </div>
    <!-- 表格 end -->

  </div>
</template>

<script>
import { showModal, showToast, deepClone, downPackFile } from "@/common/util";
import {
  GET_ACTIVITY_SEARCH,
  DELETE_ACTIVITY_INFO,
  DOWNLOAD_ACTIVITY_EXPORT,
  DOWNLOAD_ACTIVITY_QRCODE,
} from "@/http/clues";
import moment from "moment";
export default {
  data() {
    return {
      formInline: {},
      tableData: [],
      pageTotal: 0,
      pageNumber: 1,
      pageSizes: [10, 20, 50, 100],
      pageSize: 20,
      loading: false,
      eloading: false,
      DOWNLOAD_ACTIVITY_QRCODE,
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    /** 获取用户列表 */
    getList(current) {
      let _this = this;
      let oParams = _this.getParams();
      oParams.pageNumber = current || _this.pageNumber;
      _this.loading = true;
      GET_ACTIVITY_SEARCH(oParams).then((res) => {
        _this.loading = false;
        let obj = res.data || {};
        _this.tableData = obj.list || [];
        _this.pageTotal = obj.totalElements;
      });
    },
    /** 每页展示 */
    changePageSize(e) {
      this.pageSize = e;
      this.getList();
    },

    /** 获取参数 */
    getParams() {
      let o = JSON.parse(JSON.stringify(this.formInline));
      let m = o.orderTime || [];
      if (m && m.length > 0) {
        o.createdStartTime = m[0];
        o.createdEndTime = m[1];
      }

      delete o.orderTime;

      o.pageSize = this.pageSize;
      return o;
    },

    /** 重置 */
    rest() {
      this.formInline = {};
      this.pageNumber = 1;
      this.getList();
    },

    // 导出excel
    downloadExcel() {
      let _this = this;
      let o = deepClone(_this.formInline);
      let m = Object.keys(o);
      if (m.length > 0) {
        showModal({
          message: "确定是否导出筛选条件下面所有活动管理列表?",
          showCancelButton: true,
          type: "warning",
          success: function () {
            _this.dowloadExcelFile();
          },
        });
        return false;
      }

      _this.dowloadExcelFile();
    },

    // 下载excel
    dowloadExcelFile() {
      let _this = this;
      let oParams = _this.getParams();
      let options = {};
      delete oParams.pageNumber;
      delete oParams.pageSize;

      options.method = "GET";
      options.data = oParams;
      options.url = DOWNLOAD_ACTIVITY_EXPORT;
      options.fileName = "活动管理列表.xlsx";
      _this.eloading = true;
      _this.$exportExcel(options).then((res) => {
        _this.eloading = false;
      });
    },

    // 新增活动
    add(type, id) {
      let query = { type: type };
      if (id) {
        query.id = id;
      }
      this.$router.push({ name: "addActivity", query: query });
    },

    // 下载二维码
    downloadQrcode(row) {
      let options = {};
      options.method = "GET";
      options.data = { id: row.id };
      options.url = DOWNLOAD_ACTIVITY_QRCODE;
      options.fileName = `${row.name}${moment().format("yyyy-MM-DD")}.png`;
      this.$exportExcel(options);
      // downPackFile({
      //   url:DOWNLOAD_ACTIVITY_QRCODE,
      //   name:`${row.name}${moment().format('yyyy-MM-dd')}.png`
      // })
    },

    // 删除
    remove(row) {
      let _this = this;
      showModal({
        message: "确认删除该活动吗？",
        type: "warning",
        showCancelButton: true,
        success: function () {
          DELETE_ACTIVITY_INFO({ id: row.id }).then((res) => {
            if (res.code == 200) {
              showToast({
                message: "删除成功",
                type: "success",
              });
              _this.getList();
            }
          });
        },
      });
    },

    // 复制链接
    copyLink(row) {
      let input = document.createElement("input");
      input.value = row.link;
      document.body.append(input);
      input.select();
      document.execCommand("copy");
      showToast({
        message: "复制成功",
        type: "success",
      });
      document.body.removeChild(input);
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
